package cn.lctvu.servlet.admin;

import cn.lctvu.bean.ItemNews;
import cn.lctvu.bean.Items;
import cn.lctvu.bean.PageBean;
import cn.lctvu.service.ItemNewsService;
import cn.lctvu.service.ItemService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/ArticleServlet")
public class ArticleServlet extends HttpServlet {
    private ItemNewsService itemNewsService=new ItemNewsService();
    private ItemService itemService=new ItemService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //得到当前页码
        int pageNum;
        if(request.getParameter("pageNum")==null ||
                Integer.parseInt(request.getParameter("pageNum"))<1 ){
            pageNum=1;
        }else {
            pageNum=Integer.parseInt(request.getParameter("pageNum"));
        }
        //获得要查询的栏目id
        String stritemid=request.getParameter("itemid");
        if(stritemid==null){
            //如果为空，则默认查询所有
            stritemid="all";
        }
        request.setAttribute("itemid",stritemid);

        //获得要查询的标题
        String title=request.getParameter("title");

        if(title==null){
            //要查询的标题为空，就是任何标题都可以查出来。这里不按标题进行查询
            title="";
        }
        request.setAttribute("title",title);

        PageBean<ItemNews> pageBean=itemNewsService.getNewsWithPage(pageNum,2,
                stritemid,title);
        //将分页数据放在request域中
        request.setAttribute("pageBean",pageBean);
        List<Items> itemsList=itemService.findAllItems();
        request.setAttribute("itemsList",itemsList);


        request.getRequestDispatcher("article.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }
}
